<?php

namespace App\Http\Controllers;

use App\Models\User;
use App\Models\Attendance;
use App\Models\System\Admin;
use Illuminate\Support\Facades\DB;
use App\Repositories\AttendanceRepository;
use App\Http\Requests\Logic\Attendance\QueryReport;
use App\Http\Requests\Logic\Attendance\QueryResource;
use App\Http\Requests\Logic\Attendance\StoreResource;
use App\Http\Requests\Logic\Attendance\UpdateResource;
use App\Http\Requests\Logic\Attendance\QueryCollection;
use App\Http\Requests\Logic\Attendance\DestroyResource;

/**
 * 考勤模块控制器
 *
 * Date: 12/04/2018
 * @author George
 * @package App\Http\Controllers
 */
class AttendanceController extends Controller
{
	protected $attendanceRepository;

	/**
	 * AttendanceController constructor.
	 * @param AttendanceRepository $attendanceRepository
	 */
	public function __construct(AttendanceRepository $attendanceRepository)
	{
		$this->attendanceRepository = $attendanceRepository;
	}

	/**
	 * 获取考勤信息
	 *
	 * Date: 17/04/2018
	 * @author George
	 * @param QueryCollection $request
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function index(QueryCollection $request)
	{
		$user = $request->user();
		$start = $request->get('start');
		$end = $request->get('end');
		$type = $request->get('type');
		$project_id = $request->get('project_id');
		$paginate = $request->get('paginate');

		$attendances = [];

		if ($user instanceof User) {
			$attendances = $this->attendanceRepository
				->all($start, $end, $type, $project_id, $request->user()->company_id)
				->paginate($paginate);
		} elseif ($user instanceof Admin) {
			$attendances = $this->attendanceRepository->all($start, $end, $type, $project_id)->paginate($paginate);
		}
		return success($attendances);
	}

	/**
	 * 获取考勤数据详情
	 *
	 * Date: 17/04/2018
	 * @author George
	 * @param QueryResource $request
	 * @param Attendance $attendance
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function show(QueryResource $request, Attendance $attendance)
	{
		return success($attendance);
	}

	/**
	 * 创建考勤记录
	 *
	 * Date: 17/04/2018
	 * @author George
	 * @param StoreResource $request
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function store(StoreResource $request)
	{
		$attributes = $request->all();
		$attendance = $this->attendanceRepository->createOrUpdate($attributes);
		return success($attendance);
	}

	public function update(UpdateResource $request, Attendance $attendance)
	{
		// Todo 业务逻辑待定
	}

	public function destroy(DestroyResource $request)
	{
		// Todo 业务逻辑待定
	}

	public function collate()
	{

	}

	/**
	 * 获取考勤报表
	 *
	 * Date: 01/05/2018
	 * @author George
	 * @param QueryReport $request
	 * @return \Illuminate\Http\JsonResponse
	 */
    public function report(QueryReport $request)
    {
		$project_id = $request->get('project_id');
        $date = $request->get('date', date('Y-m', strtotime('-1 month')));
        $result = DB::connection('mongodb')->collection($date)
			->where('project_id', (int) $project_id)
			->get();
        return success($result);
	}

	public function export()
	{

	}
}
